package second;
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
public class CookieRW extends HttpServlet{
	private final String CNTCOOKIE="cntCookie";
	@Override
	protected void doGet(HttpServletRequest request,HttpServletResponse response)throws ServletException,IOException{
		Cookie cntCookie=null;
		int vistCnt=1;
		Cookie[] cookies=request.getCookies();
		if(cookies!=null) {
			for(int i=0;i<cookies.length;i++) {
				Cookie c=cookies[i];
				if(c.getName().equals(CNTCOOKIE)) {
					cntCookie=c;
					break;
				}
			}
		}
		if(cntCookie==null) {
			cntCookie=new Cookie(CNTCOOKIE,Integer.toString(vistCnt));
		}else {
			vistCnt=Integer.parseInt(cntCookie.getValue());
			vistCnt++;
			cntCookie.setValue(Integer.toString(vistCnt));
		}
		cntCookie.setMaxAge(60*60*24*30);
		response.addCookie(cntCookie);
		response.setContentType("text/html;cahrset=gbk");
		PrintWriter out=response.getWriter();
		out.println("<html>");
		out.println("<head>");
		out.println("<title>Cookie读写</title>");
		out.println("</head>");
		out.println("<body>");
		out.println("<p>欢迎您第<b>");
		out.println(vistCnt);
		out.println("</b>次访问本压面</p>");
		out.println("</body>");
		out.println("</html>");
	}
}	
